package org.light.shiroauth;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.exception.ValidateException;
import org.light.generator.Generator;
import org.light.utils.WriteableUtil;

public class ShiroPermsAuthorizationFilterGenerator extends Generator{
	protected Domain sampleDomain;
	
	public Domain getSampleDomain() {
		return sampleDomain;
	}

	public void setSampleDomain(Domain sampleDomain) {
		this.sampleDomain = sampleDomain;
	}

	public ShiroPermsAuthorizationFilterGenerator(Domain sampleDomain){
		super();
		this.setPackageToken(sampleDomain.getPackageToken());
		this.sampleDomain = sampleDomain;
		super.fileName = "ShiroPermsAuthorizationFilter.java";
	}
	
	@Override
	public StatementList generateStatementList() throws ValidateException {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(500L,"package "+this.getPackageToken()+".shiro;"));
		sList.add(new Statement(2000L,0,"import java.io.IOException;"));
		sList.add(new Statement(3000L,0,"import java.io.PrintWriter;"));
		sList.add(new Statement(4000L,0,"import java.util.HashMap;"));
		sList.add(new Statement(5000L,0,"import java.util.Map;"));
		if ("boot3".equalsIgnoreCase(this.sampleDomain.getTechnicalStack())||"jpa".equalsIgnoreCase(this.sampleDomain.getTechnicalStack())) {
			sList.add(new Statement(6000L,0,"import jakarta.servlet.ServletRequest;"));
			sList.add(new Statement(7000L,0,"import jakarta.servlet.ServletResponse;"));
			sList.add(new Statement(8000L,0,"import jakarta.servlet.http.HttpServletResponse;"));
		} else {
			sList.add(new Statement(6000L,0,"import javax.servlet.ServletRequest;"));
			sList.add(new Statement(7000L,0,"import javax.servlet.ServletResponse;"));
			sList.add(new Statement(8000L,0,"import javax.servlet.http.HttpServletResponse;"));
		}
		sList.add(new Statement(9000L,0,"import org.apache.shiro.subject.Subject;"));
		sList.add(new Statement(10000L,0,"import org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter;"));
		sList.add(new Statement(11000L,0,"import com.alibaba.fastjson.JSON;"));
		sList.add(new Statement(12000L,0,""));
		sList.add(new Statement(13000L,0,"public class ShiroPermsAuthorizationFilter extends PermissionsAuthorizationFilter {"));
		sList.add(new Statement(14000L,1,"@Override"));
		sList.add(new Statement(15000L,1,"protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {"));
		sList.add(new Statement(16000L,2,"Subject subject = getSubject(request, response);"));
		sList.add(new Statement(17000L,2,"HttpServletResponse res = (HttpServletResponse)response;"));
		sList.add(new Statement(18000L,2,"res.setHeader(\"Access-Control-Allow-Origin\", \"true\");"));
		sList.add(new Statement(19000L,2,"res.setContentType(\"application/json; charset=utf-8\");"));
		sList.add(new Statement(20000L,2,"res.setStatus(HttpServletResponse.SC_OK);"));
		sList.add(new Statement(21000L,2,"PrintWriter writer = res.getWriter();"));
		sList.add(new Statement(22000L,2,"Map<String, Object> map= new HashMap<>();"));
		sList.add(new Statement(23000L,2,"// 没有认证,先返回未认证的json"));
		sList.add(new Statement(24000L,2,"if (subject.getPrincipal() == null) {"));
		sList.add(new Statement(25000L,3,"map.put(\"success\", false);"));
		sList.add(new Statement(26000L,3,"map.put(\"noAuth\",true);"));
		if ("english".equalsIgnoreCase(this.sampleDomain.getLanguage())) {
			sList.add(new Statement(27000L,3,"map.put(\"error\", \"Not login.\");"));
		} else {
			sList.add(new Statement(27000L,3,"map.put(\"error\", \"未登录\");"));
		}
		sList.add(new Statement(28000L,2,"} else {"));
		sList.add(new Statement(29000L,3,"map.put(\"success\", false);"));
		sList.add(new Statement(30000L,3,"map.put(\"noAuth\",true);"));
		if ("english".equalsIgnoreCase(this.sampleDomain.getLanguage())) {
			sList.add(new Statement(31000L,3,"map.put(\"error\", \"无权限\");"));
		} else {
			sList.add(new Statement(31000L,3,"map.put(\"error\", \"No privilege.\");"));
		}
		sList.add(new Statement(32000L,2,"}"));
		sList.add(new Statement(33000L,2,"writer.write(JSON.toJSONString(map));"));
		sList.add(new Statement(34000L,2,"writer.flush();"));
		sList.add(new Statement(35000L,2,"writer.close();"));
		sList.add(new Statement(36000L,2,"return false;"));
		sList.add(new Statement(37000L,1,"}"));
		sList.add(new Statement(38000L,0,""));
		sList.add(new Statement(39000L,0,"}"));

		return WriteableUtil.merge(sList);
	}

}
